package com.cyys.modules.system.service.user;

import com.cyys.common.services.CyysBaseService;
import com.cyys.common.services.RealizeService;
import com.cyys.modules.system.dao.user.MenuDao;
import com.cyys.modules.system.dao.user.RoleDao;
import com.cyys.modules.system.dao.user.UserRoleDao;
import com.cyys.modules.system.model.user.Role;
import com.cyys.modules.system.model.user.UserRole;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("roleService")
@Transactional(readOnly = true)
public class RoleService extends CyysBaseService<RoleDao, Role> {

	@Resource
	private MenuDao menuDao;

	@Resource
	private UserRoleDao userRoleDao;

	/**
	 * 获取角色权限
	 * 
	 * @param id
	 * @return
	 * @
	 */
	public Role getById(String id)  {
		Role role = dao.selectById(id);
		role.setMenuList(menuDao.getMenuByRoleId(id));
		return role;
	}

	@Transactional(readOnly = false)
	public void setUserRole(String userId, String roleIds)  {
		// 删除原有菜单权限
		userRoleDao.deleteByUserId(userId);
		// 添加新的菜单权限
		if (roleIds != null && !roleIds.equals("")) {
			List<UserRole> list = Lists.newArrayList();
			String[] roleIdsInt = roleIds.split(",");
			for (int i = 0; i < roleIdsInt.length; i++) {
				UserRole userRole = new UserRole();
				userRole.setUserId(userId);
				userRole.setRoleId(roleIdsInt[i]);
				userRoleDao.insert(userRole);
			}
		}
	}

	// 根据用户ID查询角色（role），放入到Authorization里。
//	public Set<String> shiroByUserId(String userId) {
//		return dao.shiroByUserId(userId);
//	}

	public List<Role> getRoleByUserId(String userId) {
		return dao.getRoleByUserId(userId);
	}
//
//	public List<Role> listAllByUserId(String userId) {
//		return dao.listAllByUserId(userId);
//	}
}
